package com.jiuzhou.guanwang.jzcp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.adapter.ExpMatchAdapter;
import com.jiuzhou.guanwang.jzcp.bean.ExpDetailResult;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.vise.log.ViseLog;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.Map;

public class ExpDetailActivity extends AppCompatActivity {
    private TextView mDesTv;
    private ExpMatchAdapter mExpMacthAdapter;
    private ImageView mHeadIv;
    private TextView mHeadTv;
    private ListView mMatchLv;
    private TextView mNameTv;
    private String userId;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_exp_detail);
        initView();
        initData();
    }

    private void initView() {
        this.mHeadIv = (ImageView) findViewById(R.id.iv_head);
        this.mNameTv = (TextView) findViewById(R.id.tv_name);
        this.mDesTv = (TextView) findViewById(R.id.tv_des);
        this.mHeadTv = (TextView) findViewById(R.id.tv_head);
        this.mMatchLv = (ListView) findViewById(R.id.lv_match);
        this.mExpMacthAdapter = new ExpMatchAdapter(this);
        this.mMatchLv.setAdapter(this.mExpMacthAdapter);
    }

    private void initData() {
        this.userId = getIntent().getStringExtra("userId");

        Map<String, String> map = new HashMap<>();
        map.put("pageNo", "1");
        map.put("pageSize", "10");
        map.put("udfi", "C6680196-EBCD-4FFD-A60A-497C2FA079B1&v=D17");
        map.put("wAction", "203");
        map.put("wAgent", "0");
        map.put("userId", this.userId);
        Callback.Cancelable post = NetRequestUtil.getInstance().get("http://v.data008.com/mobile", map, 101,
                new NetRequestUtil.NetResponseListener<String>() {
                    @Override
                    public void onSuccess(String response, int requestCode) {
                        ViseLog.d(response);
                        ExpDetailResult expDetailResult = (ExpDetailResult) new Gson().fromJson(response, ExpDetailResult.class);
                        if (expDetailResult.data != null && expDetailResult.data.specialExpert != null && expDetailResult.data.articlePaging != null
                                && !CommonUtils.isEmpty(expDetailResult.data.articlePaging.item_list)) {
                            setHeadPic();
                            if (!TextUtils.isEmpty(expDetailResult.data.specialExpert.user_name)) {
                                mNameTv.setText(expDetailResult.data.specialExpert.user_name);
                            }
                            if (!TextUtils.isEmpty(expDetailResult.data.specialExpert.lable)) {
                                mDesTv.setText(expDetailResult.data.specialExpert.lable);
                            }
                            if (!TextUtils.isEmpty(expDetailResult.data.specialExpert.info)) {
                                mHeadTv.setText(expDetailResult.data.specialExpert.info);
                            }
                            mExpMacthAdapter.setData(expDetailResult.data.articlePaging.item_list);
                        }
                    }

                    @Override
                    public void onFailed(String response, int requestCode) {
                        ViseLog.e("请求失败");
                        showToast("返回失败");
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("返回解析异常");
                    }

                    @Override
                    public void onFinished() {
                    }
                });
    }
    public void showToast(String text) {
        Toast.makeText(this, "" + text, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int resId) {
        Toast.makeText(this, this.getResources().getText(resId), Toast.LENGTH_SHORT).show();
    }
    public void back(View targv) {
        finish();
    }
    private void setHeadPic() {
        String str = this.userId;
        switch (str) {
            case "500":
                this.mHeadIv.setImageResource(R.drawable.ic_head0);
                break;
            case "501":
                this.mHeadIv.setImageResource(R.drawable.ic_head1);
                break;
            case "502":
                this.mHeadIv.setImageResource(R.drawable.ic_head2);
                break;
            case "503":
                this.mHeadIv.setImageResource(R.drawable.ic_head3);
                break;
            case "513":
                this.mHeadIv.setImageResource(R.drawable.ic_head4);
                break;
            case "514":
                this.mHeadIv.setImageResource(R.drawable.ic_head5);
                break;
            case "842":
                this.mHeadIv.setImageResource(R.drawable.ic_head6);
                break;
            case "830":
                this.mHeadIv.setImageResource(R.drawable.ic_head8);
                break;
            case "26674":
                this.mHeadIv.setImageResource(R.drawable.ic_head9);
                break;
            case "564":
                this.mHeadIv.setImageResource(R.drawable.ic_head10);
                break;
            default:
                break;
        }
    }
}